﻿using Ext.Net;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CRM_Manager_ERP_SupplyManage : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState_OrderSre = "SupplyName asc";
            AddHandlersToControls();
            gridPanel_listdata.Reload();
        }
    }

    private void AddHandlersToControls()
    {
        string Url = G_Url_allstring + "/CRM_Manager/ERP_SupplyEdit.aspx";

        string tabid = base.G_FrameTabid;
        //popfortab(GridName, Url, StringName, StringField, RowId, AttQryStr, TabId, TabName, flag)
        string handler = "popfortab(gridPanel_listdata, '{0}', '{1}', '{2}', {3}, '{4}', '{5}', '{6}', {7});";
        extBtn_Add.Listeners.Click.Handler = string.Format(handler, Url, "ObjId", "Add", 0, "", tabid, "供货商（详细）", 1);
        extBtn_Edit.Listeners.Click.Handler = string.Format(handler, Url, "ObjId", "SupplyID", 0, "", tabid, "供货商（详细）", 2);
        gridPanel_listdata.Listeners.RowDblClick.Handler = string.Format(handler, Url, "ObjId", "SupplyID", "rowIndex", "", tabid, "供货商（详细）", 3);
    }

    string _QueryString;
    private string GetQryString()
    {
        string result = " 1=1 ";

        if (exttxtf_SupplyName.Text.Trim() != "")
            result += " and SupplyName like'%" + FilterSpecChar(exttxtf_SupplyName.Text.Trim()) + "%'";
        if (exttxtf_Linkman.Text.Trim() != "")
            result += " and Linkman like'%" + FilterSpecChar(exttxtf_Linkman.Text.Trim()) + "%'";
        return result;
    }

    protected void MyData_Refresh(object sender, StoreRefreshDataEventArgs e)
    {
        _QueryString = GetQryString();
        int count = CRMMis.BLL.ERP_Supply.GetRecorderCount(_QueryString);
        BindGridPSData(e, ViewState_OrderSre, PagingToolbar1, Store1, count);
    }

    public override DataTable GetPSData(int PageSize, int CurPage, string OrderStr)
    {
        ViewState_OrderSre = OrderStr;
        DataTable dt = CRMMis.BLL.ERP_Supply.GetPageList(PageSize, CurPage, _QueryString, OrderStr).Tables[0];
        return dt;
    }

    [DirectMethod]
    public void DeleteModel(int SupplyID)
    {
        try
        {
            DataTable dt = CRMMis.BLL.ERP_Cargo_Detail.GetList("SupplyId="+ SupplyID).Tables[0];
            var model = CRMMis.BLL.ERP_Supply.GetModel(SupplyID);
            if (BS_LoginRoles.Contains("02"))
            {
                if (dt.Rows.Count > 0)
                {
                    X.Msg.Alert("系统提示", "存在供应信息不能删除！").Show();
                    return;
                }
            }
            else
            {
                if (dt.Rows.Count > 0)
                {
                    X.Msg.Alert("系统提示", "存在供应信息不能删除！").Show();
                    return;
                }
                if (model.CheckState > 0)
                {
                    X.Msg.Alert("系统提示", "已提交的数据不能删除！").Show();
                    return;
                }
            }
            int r = CRMMis.BLL.ERP_Supply.Delete(SupplyID);
            if (r > 0)
            {
                X.Msg.Alert("系统提示", "删除成功！").Show();
                gridPanel_listdata.Reload();
            }
        }
        catch (Exception ex)
        {
            X.Msg.Alert("系统提示", "删除失败！</br>" + ex.Message).Show();
        }
    }
    /*
     * 本方法留着，将来可能  停用功能时使用
    [DirectMethod]
    public void CheckSupply(int SupplyID)
    {
        try
        {
            int r = CRMMis.BLL.ERP_Supply.UpdataModelByWhere(SupplyID, model =>
            {
                if (model.CheckState == 1)
                {
                    X.Msg.Alert("系统提示", "已审核的数据不需要在审核！").Show();
                    return false;
                }
                model.CheckDate = DateTime.Now.ToString();
                model.CheckUser = BS_LoginUserID;
                model.CheckState = 1;
                return true;
            });
            if (r > 0)
            {
                X.Msg.Alert("系统提示", "审核成功！").Show();
                gridPanel_listdata.Reload();
            }
        }
        catch (Exception ex)
        {
            X.Msg.Alert("系统提示", "审核失败！</br>" + ex.Message).Show();
        }
    }
    */


}